
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  event;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.Map;
import java.util.Vector;

import control.RequestParameter;
import pool.bean.IdBean;
import pool.bean.Master_table_Bean;
import pool.bean.Property_details_Bean;
import poolint.join.Operation;
import poolint.push.tablename.IntegratePool;
import utility.CapitalChar;
import dbmanager.DBManager;
import dbmanager.GetIdName;

public class Integration implements Event 
{	
	boolean RESULT = false;
	private String Product;
	private String mastertype;
	private String masterid;
	private String userid;
	private GetIdName gid;
	//private String Masterobject;
	private String TYPE = "type";
	private String PRODUCT="product";
	private String TABLE="table";
	private String INTEGRETE="integrate";
	private String TABLELIST="tablelist";
	private String Process="Process";
	private String RELATION = "relation";
	private String JOIN = "join";
	private String TABLERELATION = "tableRelation";
	
	private String pooltblName;
	//private LinkedList< Object> productlist=new LinkedList<Object>();
	private LinkedList< Object> objectList=new LinkedList<Object>();

	private Hashtable< Object, Object> relationObject = new Hashtable<Object, Object>() ;
	private RequestParameter req = new RequestParameter();
	private String POOLTABLE = "pooltable";
	
	public void SetData(String Product, String type, String masterid,
			String userid, String Process, String datetime, String request) 
	{
		this.Product = Product;
		this.mastertype = type;   /* type in nothing but table name */
		this.masterid = masterid;
		this.userid =userid;	
		this.Process = Process;/* value of process is -> add,mod,del,view*/
		req.setUsername(this.userid);
		String pwd = userid.substring(userid.length()-3);
		req.setPassword(pwd);
		req.setProcess(this.Process);
		req.setVersion("11");
		req.setMessage("");
		//req.setEvent(false);
	}
	
	public void SetData(String Product, String type, String masterid,
			String userid, String Process,String request) 
	{
		this.Product = Product;
		this.mastertype = type;   /* type in nothing but table name */
		this.masterid = masterid;
		this.userid =userid;	
		this.Process = Process;/* value of process is -> add,mod,del,view*/
		req.setUsername(this.userid);
		String pwd = userid.substring(userid.length()-3);
		req.setPassword(pwd);
		req.setProcess(this.Process);
		req.setVersion("11");
		req.setMessage("");
		//req.setEvent(false);
	}

	public boolean getResult() 
	{	
		return RESULT;
	}

	public void initializationData() 
	{
		gid = new GetIdName();
		PRODUCT = gid.getId(PRODUCT);
		TABLE = gid.getId(TABLE);
		INTEGRETE = gid.getId(INTEGRETE);
		TABLELIST =gid.getId(TABLELIST);
		RELATION = gid.getId(RELATION);
		TYPE = gid.getId(TYPE);
		JOIN = gid.getId(JOIN);
		POOLTABLE = gid.getId(POOLTABLE);
		TABLERELATION = gid.getId(TABLERELATION );
	}

	public void setObject(String Object) 
	{
		try 
		{	
		/*	if(Process.equalsIgnoreCase("add"))
			{
				RESULT = processAdd();
			}
			else if(Process.equalsIgnoreCase("mod"))
			{
				RESULT = processMod();
			}
			else if(Process.equalsIgnoreCase("del"))
			{
				RESULT = processDel();
			}*/
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		//RESULT = true;
	}
	
	private Map<? extends Object, ? extends Object> removeSameProduct(Hashtable<Object, Object> relationObjectHash) 
	{
		String query = "Select d3.pv from property_details d3 JOIN(property_details d JOIN property_details d2 ON (d.mid=d2.mid)) ON (d2.mid = d3.mid) " +
		" where d.pid = "+TABLE+" and  d2.pid = "+TYPE+" and d2.pv= "+TABLELIST+" and d.pv = "+mastertype+" and d3.pid = "+TABLE;
		ResultSet resultset = DBManager.getSelect(query);
		
		try 
		{
			while(resultset.next())
			{
				String tabletype = resultset.getString("pv");
				if(relationObjectHash.containsKey(tabletype)||relationObjectHash.containsKey(TYPE))
					relationObjectHash.remove(tabletype);
			}
			if(relationObjectHash.containsKey(TYPE))
				relationObjectHash.remove(TYPE);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return relationObjectHash;
	}


	
	private Hashtable<Object,Object> getRelationObjectHash(String masterid) 
	{
		Hashtable<Object,Object> hash = new Hashtable<Object, Object>();
		String TABLERELATION = "tableRelation";
		TABLERELATION = gid.getId(TABLERELATION );
		String query = "SELECT DISTINCT d2.pid,d2.pv,d2.vt from property_details d2 JOIN( property_details d JOIN property_details d1 ON (d.mid=d1.mid))ON(d2.mid=d1.mid) ";
		query = query + " where d.pid = " + TYPE + " AND d.pv = " + TABLERELATION + " AND "; //d1.pv = " + masterid;
				
		if(gid.getId(masterid).equals(""))
		{
			query = query + " d1.pv = '" + masterid + "'";
		}
		else
		{
			query = query + " d1.vt = '" + masterid + "'";
		}
		
		ResultSet rs = DBManager.getSelect(query);
		try 
		{
			while(rs.next())
			{
				String strVtPv = gid.getItem(rs.getString("pv"));
				String strPID = rs.getString("pid");
				if(strVtPv.equalsIgnoreCase("no$") || strVtPv.equals("0"))
				{
					//strVtPv = gid.getItem(rs.getString("vt"));
					strVtPv = rs.getString("vt");
				}
				if(!gid.getItem(strPID).equalsIgnoreCase("TYPE")) //for "type = tableRelation" not to be inserted into Hash. 
					hash.put(rs.getString("pid"), strVtPv);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return hash;
	}

	
	@SuppressWarnings("unchecked")
	String insertObject(String tableobj,String masterObj,Hashtable<Object, Object> fieldHash,Hashtable<Object, Object> poolHash2)
	{
		poolint.join.Operation operation;
		try
		{
			 pooltblName = getPoolTablename(tableobj);
			 
			Class c = Class.forName("poolint.join."+CapitalChar.makeFirstCharCapital(Process));
			operation = (poolint.join.Operation) c.newInstance();
			
			operation.setTableObj(tableobj);
			operation.setPoolTablename(pooltblName);
			operation.setRecentTableObject(masterid);
			operation.setPoolHash(poolHash2);
			operation.generateProperty();
			operation.setFields(fieldHash);
			operation.setMaster(masterObj);
			operation.getPropertyVector();			
			return operation.performOperation();
		} 
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (Exception e) {
		}
		return null;
	}
	
	
	@SuppressWarnings("unchecked")
	void processQuery(String type, String master)
	{
		
		poolint.pushdata.Operation operation;
		 pooltblName = getPoolTablename(type);
		try 
		{
			Class c = Class.forName("poolint.pushdata."+CapitalChar.makeFirstCharCapital(Process));
			operation = (poolint.pushdata.Operation) c.newInstance();
			operation.setRecentTableObject(master);
			operation.setTableObj(type);
			operation.setPoolTablename(pooltblName);
			operation.performOperation();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	@SuppressWarnings("unchecked")
	private Hashtable<Object, Object> getPoolHash(String joinobj, String tablobj, String masterid2) 
	{
		String LHS = gid.getId("LHS");     // treated as inside Outside Object
		String RHS = gid.getId("RHS");		// treated as inside Inside Object
		String fieldname=null;
		String key = null;
		String id = null;
		Hashtable<Object,Object> hashtable = new Hashtable<Object, Object>();
		
		ResultSet rsPoolValue = DBManager.getSelect("SELECT mid, pid, pv, vt FROM property_details WHERE mid = " + joinobj + " and pid in("+LHS+","+RHS+")");
		try 
		{
			while(rsPoolValue.next())
			{
				if(rsPoolValue.getString("pid").equalsIgnoreCase(LHS))
				{
					key = rsPoolValue.getString("pv");
				}
				else if(rsPoolValue.getString("pid").equalsIgnoreCase(RHS))
				{
					fieldname = rsPoolValue.getString("pv");
				}
			}
			rsPoolValue.close();		
			
			/*rsPoolValue = DBManager.getSelect("SELECT mid, pid, pv, vt FROM property_details  WHERE mid = " + masterid2 + " and pid ="+key);
			rsPoolValue.next();*/
			// get the object from the property_details where id = id_var and type = tableobj then call to insert master and generatePoolHash() function
					
			pooltblName = getPoolTablename(tablobj);
			try 
			{					
				hashtable.put("masterid", masterid2);
				hashtable.put("key", key);
				hashtable.put("fieldname", fieldname);
				hashtable.put("TYPE", TYPE);
				hashtable.put("tablobj", tablobj);	
				
				Class c1 = Class.forName("poolint.push.tablename."+CapitalChar.makeFirstCharCapital(pooltblName));
				IntegratePool integrate = (IntegratePool) c1.newInstance();	
				id=integrate.getID(hashtable);						
			}
			catch (Exception e) {
				e.printStackTrace();
			}				
			
			
			if(id!=null)
			{
				/*id = rsPoolValue.getString("pv");
				if(id.equalsIgnoreCase("0"))
				{
					id = rsPoolValue.getString("vt");
					rsPoolValue.close();
					rsPoolValue = DBManager.getSelect("SELECT p.mid, p.pid, p.pv, p.vt FROM property_details p JOIN property_details p1 ON(p.mid = p1.mid)  WHERE p.pid = " + fieldname + " and p.vt ="+id + " and p1.pid ="+TYPE+" and p1.pv="+tablobj);
				}
				else
				{
					rsPoolValue.close();
					rsPoolValue = database.getSelect("SELECT p.mid, p.pid, p.pv, p.vt FROM property_details p JOIN property_details p1 ON(p.mid = p1.mid)  WHERE p.pid = " + fieldname + " and p.pv ="+id + " and p1.pid ="+TYPE+" and p1.pv="+tablobj);
				}
				rsPoolValue.next();
				String mid = rsPoolValue.getString("mid");
				
				relationObject.put(gid.getItem(tablobj), gid.getItem(mid));*/
							
				relationObject.put(gid.getItem(tablobj),id);
				try 
				{
					Class c = Class.forName("poolint.join."+CapitalChar.makeFirstCharCapital(Process));
					Operation operation = (Operation) c.newInstance();				
					operation.setTableObj(tablobj);
					//operation.setRecentTableObject(mid);
					operation.setRecentTableObject(id);
					operation.setPoolTablename(pooltblName);
					return operation.getPoolHash();
				}
				catch (ClassNotFoundException e) {
					e.printStackTrace();
				} catch (InstantiationException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
		} 
		catch (SQLException e) {
		}
		return new Hashtable<Object, Object>();
	}
	
	
	
	@SuppressWarnings("unchecked")
	private Hashtable<Object, Object> checkJoin(String master, String joinobj)
	{
		Hashtable<Object, Object> fieldHash = new Hashtable<Object, Object>();
		String LHS = gid.getId("LHS"); 		// treated as inside Outside Object
		String RHS = gid.getId("RHS");		// treated as inside Inside Object
		String fieldname=null;
		String key = null;
		
		ResultSet rsPoolValue = DBManager.getSelect("SELECT mid, pid, pv, vt FROM property_details  WHERE mid = " + joinobj + " and pid in("+LHS+","+RHS+")");
		
		try 
		{
			while(rsPoolValue.next())
			{
				if(rsPoolValue.getString("pid").equalsIgnoreCase(LHS))
				{
					//fieldname = gid.getItem(rsPoolValue.getString("pv"));
					fieldname = rsPoolValue.getString("pv");
				}
				else if(rsPoolValue.getString("pid").equalsIgnoreCase(RHS))
				{
					key = rsPoolValue.getString("pv");
				}
			}
				
			String polTblname="";
			rsPoolValue = DBManager.getSelect("SELECT pv FROM property_details  WHERE mid = " + joinobj + " and pid ="+TABLE);
			try {
				rsPoolValue.next();
				polTblname = rsPoolValue.getString(1);
			} catch (Exception e) {
				// TODO: handle exception
			}		
			
			pooltblName = getPoolTablename(polTblname);
				try 
				{	
					
					Class c = Class.forName("poolint.push.tablename."+CapitalChar.makeFirstCharCapital(pooltblName));
					IntegratePool integrate = (IntegratePool) c.newInstance();	
					integrate.setTableObj(mastertype);
					fieldname = integrate.getFieldValue(master,fieldname);	
					//fieldname = gid.getItem(fieldname);
				}
				catch (Exception e) {
					e.printStackTrace();
				}				
				rsPoolValue.close();
			/*
			rsPoolValue = DBManager.getSelect("SELECT mid, pid, pv, vt FROM property_details  WHERE mid = " + master + " and pid ="+fieldname);
			rsPoolValue.next();
			fieldname = rsPoolValue.getString("pv");
			if(fieldname.equals("0"))
				fieldname = rsPoolValue.getString("vt");
			else
				fieldname = gid.getItem(fieldname);*/
			fieldHash.put(gid.getItem(key),fieldname);
		} 
		catch (SQLException e) { }
		
		return fieldHash;
	}
	
	
	private String getProduct(String object) 
	{
		try 
		{
			ResultSet rs = DBManager.getSelect("select d3.mid as product from property_details d3 JOIN(property_details d JOIN property_details d2 on (d.mid=d2.mid))ON (d.mid=d3.pv)" +
					"where d.pid = "+TABLE+" and d.pv= "+object+" and  d2.pid = "+TYPE+" and d2.pv= "+TABLELIST+" and d3.pid = "+TABLELIST);
			rs.next();
			object = rs.getString("product");
			rs.close();
		} 
		catch (SQLException e) {
			e.printStackTrace();
		}
		return object;
	}
	
	private String getPoolTablename(String tableName) 
	{
		String table;
		try
		{
			String query = "select pv from property_details where mid = "+tableName+" and pid = "+POOLTABLE;
			ResultSet rs1 = DBManager.getSelect(query);
			rs1.next();
			table = rs1.getString(1);	
			table = gid.getItem(table);
		} 
		catch (Exception e) {
			table = "Master_table";
		}
		return table;
	}
	
	private boolean processAdd()
	{
		boolean addFlag = false;
		Hashtable<Object, Object> poolHash = new Hashtable<Object, Object>();		
		try
		{
			ResultSet rs = null;

			String query = "SELECT DISTINCT d3.pv as object from property_details d3 JOIN(property_details d JOIN property_details d2 on (d.mid=d2.mid))ON (d.mid=d3.mid)" +
							"where d.pid = "+TABLE+" and  d2.pid = "+TYPE+" and d2.pv= "+RELATION+" and d.pv= "+mastertype+" and d3.pid = "+TABLE;
			ResultSet resultset = DBManager.getSelect(query);
			
			resultset.next();
			
			try 
			{
				if(!resultset.getString("object").equals(""))
				{
					resultset.previous();
					
					//String objStr = "";
					Product = gid.getId(Product);
					while(resultset.next())
					{
						String object = resultset.getString("object").toString();
						
						String product = getProduct(object);
						if(!product.equalsIgnoreCase(Product))
						{
							if(!objectList.contains(object))
							{
								objectList.add(object);
								//objStr = objStr + object + ",";
							}
						}
						else 
						{
							rs = DBManager.getSelect("select p.pv as joinobj, q.pv as tableobj from property_details p JOIN property_details q ON(p.pv = q.mid) where p.mid="+object+" and p.pid = "+gid.getId("JOIN")+" and q.pid="+gid.getId("table"));
							while(rs.next())
							{
								String joinobj = rs .getString("joinobj");
								String tablobj = rs.getString("tableobj");
								poolHash.putAll(getPoolHash(joinobj,tablobj,masterid)); 
							}
							rs.close();
						}
					}
					resultset.close();
				}
			} 
			catch (Exception e1) 
			{
				System.out.println("\n\n No Relation is Present in the Object.... \n\n");
				
				/** SELECT p1.mid FROM property_details p1 JOIN (property_details p2 JOIN property_details p3 
				ON (p3.mid = p2.mid)) ON (p2.mid = p1.pv) 
				where p3.pv = 8456 and p3.pid = 2223
				and p2.pid = 9 and p2.pv = 8449 and p1.pid = 8449 returns Parent object**/
				
				query = "SELECT p1.mid FROM property_details p1 JOIN (property_details p2 JOIN property_details p3" + 
				" ON (p3.mid = p2.mid)) ON (p2.mid = p1.pv)" + " WHERE p3.pv = " + mastertype + " AND p3.pid = " + TABLE + " AND p2.pid = " + TYPE + " AND p2.pv = " + JOIN +
				" AND p1.pid = " + JOIN; 
				
				rs = DBManager.getSelect(query);					
				try 
				{
					rs.next();
					if(!rs.getString("mid").equals(""))
					{
						rs.previous();
						while(rs.next())
						{
							String parentObj = rs.getString("mid");
							
							String LHS = gid.getId("LHS");
							
							/** SELECT p3.pv FROM property_details p1 JOIN (property_details p2 JOIN property_details p3 
								ON (p2.mid = p3.mid)) ON (p3.mid = p1.mid)
								where p1.pid = 9 and p1.pv = 8449 and p2.pv = 8456 and p2.pid = 2223 and p3.pid = 8452 
								returns L.H.S. value.
							**/
							query = "SELECT p3.pv as foreignkey FROM property_details p1 JOIN (property_details p2 JOIN property_details p3 " + 
								"ON (p2.mid = p3.mid)) ON (p3.mid = p1.mid)" +
								" WHERE p1.pid = " + TYPE + " AND p1.pv = " + JOIN + " AND p2.pv = " + mastertype + " AND p2.pid = " + TABLE + " AND p3.pid = " + LHS ;
							
							ResultSet rsLHS = DBManager.getSelect(query);
							while(rsLHS.next())
							{
								String foreignkey = rsLHS.getString("foreignkey");
								ResultSet rsChild = DBManager.getSelect("SELECT pv, vt FROM property_details WHERE mid = " + masterid + " AND pid = " + foreignkey);
								rsChild.next();
														
								/**  SELECT p2.mid FROM property_details p1 JOIN property_details p2 ON (p2.mid = p1.mid)
								where p1.pid = 9 and p1.pv = 8405 and p2.vt = '1000091' and p2.pid = 8381 
								returns current parent object.  **/
								
								query = "SELECT p2.mid FROM property_details p1 JOIN property_details p2 ON (p2.mid = p1.mid) " +
						        "WHERE p1.pid = " + TYPE + " AND p1.pv = " + parentObj + " AND p2.pid = " + foreignkey + " AND ";
								
								String strForeignValue = rsChild.getString("pv");
								if(strForeignValue.equals("0"))
								{
									strForeignValue = rsChild.getString("vt");
									query = query + "p2.vt = " + strForeignValue;
								}
								else
								{
									//strForeignValue = rsChild.getString("vt");
									query = query + "p2.pv = " + strForeignValue;
								}
								rsChild.close();
								ResultSet rsParent = DBManager.getSelect(query);
								rsParent.next();
								String currentParent = rsParent.getString("mid");
								rsParent.close();
								query = "SELECT d2.mid from property_details d2 JOIN( property_details d JOIN property_details d1 ON (d.mid=d1.mid))ON(d2.mid=d1.mid) " +
								 		" WHERE d.pid = " + TYPE + " AND d.pv = " + TABLERELATION + " AND d1.vt = " + currentParent;
								
								ResultSet rsRelation = DBManager.getSelect(query);	
								rsRelation.next();
								String mid = rsRelation.getString("mid");
								Hashtable<Object, Object> tempHash = new Hashtable<Object, Object>();
								rsRelation.close();
								tempHash.put("mid", gid.getItem(mid));
								tempHash.put("pid", gid.getItem(mastertype));
								tempHash.put("pv", masterid);
								
								/*Vector<Object> vector = new Vector<Object>();
								vector.add(tempHash);*/
								
						//		RequestParameter req = new RequestParameter();
								req.setRequest("property_details");
								
								Property_details_Bean prop = new Property_details_Bean();
								prop.setMid(gid.getItem(mid));
								prop.setProcess(Process);
								prop.setReqParam(req);
								prop.setTable("property_details");
								prop.setPropertyHash(tempHash);
								prop.insert();
								System.out.println("\n Table Relation got Inserted.... \n"); 
							}
							rsLHS.close();
							//Mod process for the current master objects.
							Process = "mod";
							if(processMod())
								addFlag = true;					
						}
						if(addFlag)
						{
							return addFlag;
						}
					}
				} 
				catch (Exception e) 
				{
					System.out.println("\n\n No Parent Found...... \n\n");
					//Going for INSERT process. By assigning object list to the masterId. 
					//objectList.add(masterid);		
				}			
				
			}
				
			Hashtable<Object, Object> fieldHash = new Hashtable<Object, Object>();
			boolean RelationFlag = false;
			for(int i=0;i<objectList.size();i++)
			{
				String integrationobj = objectList.get(i).toString();
				
				String parentMaster;
				//master = isertMaster(integrationobj,"", fieldHash,poolHash);
				parentMaster = insertObject(integrationobj,masterid, new Hashtable<Object, Object>(),poolHash);
				
				if (!(parentMaster.equals("0")||parentMaster.equals("no$")||parentMaster==null))
				{
					relationObject.put(gid.getItem(integrationobj),parentMaster);
					//parentMaster = gid.getId(parentMaster);
					processQuery(integrationobj, parentMaster);
					resultset = DBManager.getSelect("select p.pv as joinobj, q.pv as tableobj from property_details p JOIN property_details q ON(p.pv = q.mid) where p.mid="
									+ integrationobj+ " and p.pid = "+ gid.getId("JOIN")+ " and q.pid="+ gid.getId("table"));
					while (resultset.next()) 
					{
						try
						{
							String joinobj = resultset.getString("joinobj");
							String tablobj = resultset.getString("tableobj");
							fieldHash.putAll(checkJoin(parentMaster, joinobj));
							//master = isertMaster(tablobj,"",new Hashtable<Object, Object>(),poolHash);

							String master = insertObject(tablobj, masterid,
									fieldHash, poolHash);
							//Call to checkJoin and pass joinobject and name of the master
							relationObject.put(gid.getItem(tablobj), master);
							//master = gid.getId(master);
							processQuery(tablobj, master);
							//fieldHash.putAll(checkJoin(master,joinobj));
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
					resultset.close();
				}
				/*master = isertMaster(integrationobj,"", fieldHash,poolHash);
				relationObject.put(gid.getItem(integrationobj), master);
				master = gid.getId(master);
				processQuery(integrationobj, master);*/
				RelationFlag  = true;
			}
			if(RelationFlag)
			{
				//relationObject.put(gid.getItem(mastertype), gid.getItem(masterid));
				relationObject.put(gid.getItem(mastertype), masterid);
				Vector<Object> propertyVect = new Vector<Object>();
				/** It will insert into M & PL of the POOLDB. **/
				Enumeration<Object> keys = relationObject.keys();
				while(keys.hasMoreElements())
				{
					String key = keys.nextElement().toString();
					String value = relationObject.get(key).toString();
					Hashtable<Object, Object> hash = new Hashtable<Object, Object>();
					hash.put("pid", key);
					hash.put("pv", value);
					propertyVect.add(hash);
				}
				
				Hashtable<Object, Object> hash = new Hashtable<Object, Object>();
				hash.put("pid", "type");
				hash.put("pv", "tableRelation");
				propertyVect.add(hash);
			
				IdBean idObj = new IdBean();
				idObj.setTable("master_table");
				idObj.execute();
				String id = idObj.getId();
				
				
			
				/*req.setUsername("+911234567891");   declared globally...
				req.setPassword("891");
				req.setProcess("add");
				req.setVersion("11");
				req.setMessage("");*/
				
				req.setRequest("master_table");
				
				String master1 = "tableRelation_" + id;
				Master_table_Bean mtb = new Master_table_Bean();
				mtb.setReqParam(req);
				mtb.setNewMasterObject(master1);
	//				getPropertyVector();
				mtb.setPropertyVector(propertyVect);
				mtb.insert();
				
				addFlag = true;
				return addFlag;
			}
			
		} catch (Exception e) { }
		
		return true;
	}
	
	
	
	private boolean processMod()
	{
		boolean modFlag = false;
		try
		{
			Hashtable<Object, Object> poolHash = new Hashtable<Object, Object>();		
			Hashtable<Object, Object> fieldHash = new Hashtable<Object, Object>();
			Hashtable<Object, Object> relationObjectHash = new Hashtable<Object, Object>();
			relationObjectHash.putAll(getRelationObjectHash(masterid));
			if(!Product.equalsIgnoreCase("pool"))
				relationObjectHash.putAll(removeSameProduct(relationObjectHash));
			
			Enumeration< Object> enumerator = relationObjectHash.keys();
			while(enumerator.hasMoreElements())
			{
				String integrationobj = enumerator.nextElement().toString();
				
				String master;
				master = relationObjectHash.get(integrationobj).toString();
				master = insertObject(integrationobj, master,fieldHash,poolHash);
			//	master = gid.getId(master);
				processQuery(integrationobj, master);
				modFlag = true;			
			}
			modFlag = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
		return modFlag;
	}
	
	
	
	private boolean processDel()
	{
		boolean delFlag = false;
		try 
		{
			String query = "select d3.pv as object from property_details d3 JOIN(property_details d JOIN property_details d2 on (d.mid=d2.mid))ON (d.mid=d3.mid)" +
			"where d.pid = "+TABLE+" and  d2.pid = "+TYPE+" and d2.pv= "+RELATION+" and d.pv= "+mastertype+" and d3.pid = "+TABLE;
			ResultSet resultset = DBManager.getSelect(query);
			
			//Product = gid.getId(Product);
			while(resultset.next())
			{
				String object = resultset.getString("object").toString();
				
				String product = getProduct(object);
				if(!product.equalsIgnoreCase(Product))
				{
					if(!objectList.contains(object))
					{
						objectList.add(object);
					}
				}
			}
			resultset.close();
			
			Hashtable<Object, Object> relationObjectHash = new Hashtable<Object, Object>();
			relationObjectHash.putAll(getRelationObjectHash(masterid));
			if(!Product.equalsIgnoreCase("pool"))
				relationObjectHash.putAll(removeSameProduct(relationObjectHash));
			
			Enumeration< Object> enumerator = relationObjectHash.keys();
			if(objectList.contains(mastertype))
			{
				while(enumerator.hasMoreElements())
				{
					String integrationobj = enumerator.nextElement().toString();
					
					String master;
					master = relationObjectHash.get(integrationobj).toString();
					//master = isertMaster(integrationobj, master,fieldHash,poolHash);
				  	//master = gid.getId(master);
					processQuery(integrationobj, master);
				}	
			}
			else
			{
				while(enumerator.hasMoreElements())
				{
					String integrationobj = enumerator.nextElement().toString();
					
					String master;
					master = relationObjectHash.get(integrationobj).toString();
					//Process = "del";
					//master = isertMaster(integrationobj, master,fieldHash,poolHash);
					//master = gid.getId(master);
					Process = "mod";
					processQuery(integrationobj, master);
				}
			}		
			delFlag = true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return delFlag;
	}


	public boolean afterAdd() 
	{
		return 	processAdd();
	}

	public boolean afterDel() 
	{
		return processDel();
	}

	public boolean afterMod() 
	{
		return processMod();
	}

	public boolean afterView() 
	{
		return false;
	}

	
	
	/** It will returns the parent object of the given object.*/
	/*private String getParent(String object) 
	{
		//select d3.mid  object from property_details d3 JOIN (property_details d JOIN property_details d2 on
		//(d.mid=d2.mid))ON (d.mid=d3.pv) where d.pid = 9 and d.pv = 8449 and d2.pid = 2223 and d2.pv = 8405
		//String joinID = gid.getId(JOIN); 
		String query = "SELECT d3.mid parent from property_details d3 JOIN (property_details d JOIN property_details d2 ";
		query = query + "ON (d.mid=d2.mid)) ON (d.mid=d3.pv) WHERE p.pid = " + TYPE + " AND d.pv = " + JOIN + " AND d2.pid = " + TABLE + " AND d2.pv = " + object;
		
		ResultSet rsParent = DBManager.getSelect(query);
		try 
		{
			rsParent.next();
			return rsParent.getString("mid");
		} catch (Exception e) {
			System.out.println(" Parent Not Found....");
			e.printStackTrace();
		}
		return null;		
	}*/
	
	/*private String getObjectList(String object) 
	{
		String joinID = gid.getId(JOIN); 
		String query = "SELECT d3.mid parent from property_details d3 JOIN (property_details d JOIN property_details d2 ";
		query = query + "ON (d.mid=d2.mid)) ON (d.mid=d3.pv) WHERE p.pid = " + TYPE + " AND d.pv = " + joinID + " AND d2.pid = " + TABLE + " AND d2.pv = " + object;
		query = query + "";
		
		return null;				
	}*/
	
	/*public static void main(String[] args) {
		Integration integ = new Integration();
		DBManager db = new DBManager();
		db.getConnect();
		integ.initializationData(db);
		integ.SetData("SugarpoolObject", "8413", "8509", "+911234567891", "add");
		System.out.println(integ.getPoolHash("8451", "8437","8509"));
	}*/
}
